package com.ruoyi.livedemo.mapper;

import com.ruoyi.livedemo.entity.Reminder;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ReminderMapper {

    @Select("SELECT * FROM reminders WHERE user_id=#{userId} ORDER BY remind_at IS NULL, remind_at, id")
    List<Reminder> findByUser(Long userId);

    @Insert("INSERT INTO reminders (user_id, title, message, remind_at, priority, category, acknowledged, cover_path) " +
            "VALUES (#{userId}, #{title}, #{message}, #{remindAt}, #{priority}, #{category}, #{acknowledged}, #{coverPath})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Reminder reminder);

    @Select("SELECT * FROM reminders WHERE id=#{id} AND user_id=#{userId}")
    Reminder findByIdAndUser(@Param("id") Long id, @Param("userId") Long userId);

    @Update("UPDATE reminders SET title=#{title}, message=#{message}, remind_at=#{remindAt}, " +
            "priority=#{priority}, category=#{category}, acknowledged=#{acknowledged}, cover_path=#{coverPath} " +
            "WHERE id=#{id} AND user_id=#{userId}")
    int update(Reminder reminder);

    @Delete("DELETE FROM reminders WHERE id=#{id} AND user_id=#{userId}")
    int delete(@Param("id") Long id, @Param("userId") Long userId);

    @Select("SELECT * FROM reminders WHERE user_id=#{userId} ORDER BY remind_at IS NULL, remind_at, id LIMIT #{limit} OFFSET #{offset}")
    List<Reminder> findPage(@Param("userId") Long userId,
                            @Param("offset") int offset,
                            @Param("limit") int limit);

    @Select("SELECT COUNT(1) FROM reminders WHERE user_id=#{userId}")
    long countByUser(Long userId);
}
